/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2017-08-22 09:45:07
 * @Description: 
 */

import Vue from 'vue'
import Router from 'vue-router'
import permissionPartMixin from '../common/permissionPartMixin'

Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: __dirname,
    /* 
    //savedPosition 属性 浏览器回退前进有效，模块内部跳转无效，无法满足需求，模块内部自主维护滚动条状态 https://segmentfault.com/q/1010000007408113
    scrollBehavior(to, from, savedPosition) {
        console.log('scrollBehavior', to, from, savedPosition);
        if (savedPosition) {
            return savedPosition
        } else {
            const position = {}
            if (to.hash) {
                position.selector = to.hash
            }
            if (to.matched.some(m => m.meta.scrollToTop)) {
                position.x = 0
                position.y = 0
            }
            return position
        }
    },
    */
})

//{
//path: '*/pay:key([0-9a-z]{32})',
//name: 'checkPayStatus',
//component: CheckPayStatus
//}

router.beforeEach((to, from, next) => {

    if (!(_.indexOf(Config.PAGE_NO_ACCESS, to.name) > -1)) {
        if (!Unit.getCookie('client_key') || !Unit.getSessionStorage("ms_myself")) {
            console.log('no access go to login!');
            next({ name: 'login' });
        } else {
            if (parseInt(Unit.getSessionStorage('ms_agree')) == 1) {
                if (_.indexOf(Config.PAGE_CHECK_PROFILE, to.name) > -1) {
                    if (permissionPartMixin.methods.permissionPartMixin_checkCompleteProfile()) {
                        next();
                    } else {
                        next({ name: 'completeProfile' });
                    }
                } else {
                    next();
                }
            } else {
                to.name == 'agreeServicePrivacy' ? next() : next({ name: 'agreeServicePrivacy' });
            }
        }
    } else {
        //no access page need get tid
        let tid = to.query.tid;
        if (tid) {
            Unit.setCookie('tid', tid, { path: '/', domain: Unit.getRootDomain() });
        }
        next();
    }

    if (to.name != 'upgrade' && to.name != 'checkPayStatus' && to.name != 'gotoCheckPayStatus') {
        Unit.setSessionStorage('cur_url', to.fullPath);
    }
    if (Unit.getSessionStorage("paying") == 'true' && to.name != 'checkPayStatus') {
        //router.push('/checkPayStatus');
        next({ name: 'checkPayStatus' });
    }

})

/******** after Each *******/
var requestThrottle;
router.afterEach((route) => {
    let stateArr = ['chat', 'connections', 'me', 'letsMeet'];
    //console.log(route.name, router.app.$store, stateArr.indexOf(route.name) > -1);
    //fist $store have not
    if (router.app.$store && stateArr.indexOf(route.name) > -1) {
        if (!requestThrottle) {
            requestThrottle = _.throttle(function() { router.app.$store.dispatch('requestSiteNumStateData', {}) }, 60000);
        }
        requestThrottle();
        //router.app.$store.dispatch('requestSiteNumStateData', {});
    }
})

export default router;
